auto.waitFor()
var appRun = currentActivity()
var t=0

let taskList = ["应用商店","i视频","游戏中心","i音乐","i主题","钱包","快应用"]
/**
 * 上滑
 */
 function swipe_my(){

    // random_time = getRndInteger(500,1500)
    
    // swipe(500, 1800, 500, 800, random(200,1500))

    swipe(500, 1800, 500, 1300, random(200,1500))
    
    // gesture(1000, [600, 1600], [500, 1000])

}

/**
 * 左滑
 */
 function swipe_left_to_visible(btn){
    var position = btn.bounds();
    position.centerX()
    
    toastLog(position.centerX() - 1000)

    // random_time = getRndInteger(500,1500)
    
    // swipe(500, 1800, 500, 800, random(200,1500))
    let times = (position.centerX() - 1000)/300 
    for (let t = 0; t < times; t++) {
        swipe(500, position.centerY(), 200, position.centerY(), random(200,500))
    }

    // gesture(1000, [600, 1600], [500, 1000])

}
/**
 * 
 * 通过bounds 点击
 * @param {*} btn  按钮对象
 */
 function clickBtnByBoundsCenter(btn) {
    var position = btn.bounds();
    press(position.centerX(), position.centerY(),1);
    sleep(1000);
}

/**
 * 是否可见的按钮
 * @param {} btn 
 */
function isVisiableBtn(btn){
    let btn = text(taskList[4]).findOne(1000)
    if(btn.centerX>1100){
        return false;
    }
}

/**
 * 进入 主页面
 */
function enterMain(){
    app.openUrl("https://pointh5.vivo.com.cn/#/taskList")
    sleep(2000)
    swipe_my()
    
}

function checkAllComplete(){
    for(let i=0; i<taskList.length;i++){
        let taskName = taskList[i]
        if(textEndsWith(taskName).exists()){
            toastLog("找到任务----"+taskName);
        }
        let btn = text(taskName).findOne(1000)

        if(!isVisiableBtn(btn)){
            swipe_left_to_visible(btn)
            sleep(200)

            btn = text(taskName).findOne(1000)
        }

        clickBtnByBoundsCenter(btn)

        sleep(5000)
        enterMain()
        sleep(1000)

    }

    
}

/**
 * 清除后台
 */
function clearBackApp(){
    home()
    sleep(1000)
    
    recents()
    
    sleep(2000)
    
    let cleanBackBtn = desc("清除全部-按钮").findOne(2000)
    clickBtnByBoundsCenter(cleanBackBtn)
}

function jiaofeiSign(){
    let btn = desc("我的账号").depth(20).findOne(3000)
    clickBtnByBoundsCenter(btn)

    desc("签到有奖").findOne(3000).click()

    let btn = desc("我的账号").findOne(3000)
    clickBtnByBoundsCenter(btn)
}

function jiaofei(){
    app.openUrl("https://qapp-h5.vivo.com.cn/?packageName=com.vivo.living&path=&shareUrl=&params=")
    sleep(3000)

    jiaofeiSign()
    // 电费
    let taskName = "电费"
    if(textEndsWith(taskName).exists()){
        let btn = text(taskName).find()
        if(btn.length>1){
            clickBtnByBoundsCenter(btn[btn.length-1]);
        }else{
            clickBtnByBoundsCenter(btn);
        }

        sleep(2000)
    }else{
        toastLog("未找到"+taskName);
        return;
    }
  

    // 选择缴费项目
    let city = "贵州省电费"
    if(textEndsWith(city).exists()){
        let cityBtn = text(city).findOne(1000)
        clickBtnByBoundsCenter(cityBtn)

        sleep(3000)
    }else{
        toastLog("未找到"+city);
        return;
    }
   
      // 输入客户编号
    let code = "1-20位客户编号"
    if(textEndsWith(code).exists()){
        setText(0,"0601010027846849")
        sleep(1000)

        clickBtnByBoundsCenter(text("下一步").findOne(1000))
        sleep(4000)
    }else{
        toastLog("未找到"+code);
        return;
    }   

    // 缴费金额
    setText(0,"0.01")
    clickBtnByBoundsCenter(text("立即缴费").findOne(1000))
    sleep(2000)


}


function browserSign(){
    app.openUrl("https://topic.vivo.com.cn/point/TP4tvkhtu4z4o0/index.html")
    sleep(3000)
}

function myIqoo(){
    let pkgName = "com.vivo.usercenter"
    toast("开始" + pkgName)
    launch(pkgName)
    sleep(4000)

    id("check_in_container").click()
}

function start(){
    enterMain()

    checkAllComplete()

    browserSign()

    myIqoo()
}


start()

for (let index = 0; index < 3; index++) {
    toastLog("第"+index+"次缴费");
    jiaofei()    
}